package cn.gson.crm.controller.relation;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.gson.crm.model.dao.ImagesDao;
import cn.gson.crm.model.dao.StudentDao;
import cn.gson.crm.model.dao.StudentInformationDao;
import cn.gson.crm.model.dao.TeaemploymentDao;
import cn.gson.crm.model.entity.JyTeaemployment;
import cn.gson.crm.model.entity.Payrecord;
import cn.gson.crm.model.entity.Student;
import cn.gson.crm.model.mapper.StudentInformationMapper;
import cn.gson.crm.model.mapper.TeaemploymentMapper;

/**
 * 
 * @author GHOST
 *
 */
@Controller
@RequestMapping("/relation")
public class StudentInformationController {


	@Autowired
	private StudentInformationMapper stuinforMapper;
	
	@Autowired
	private StudentInformationDao stuinforDao;

	@Autowired
	private TeaemploymentMapper teaMapper;
	
	@Autowired
	private TeaemploymentDao teaDao;
	
	@Autowired
	private ImagesDao imgDao;
	
	/**
	 * 上传图片路径
	 */
	@Value("${upload.file}")
	private String uploadDir;
	/**
	 * 查询所有学员的就业反馈信息
	 * @param stuinfor
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("list")
	@ResponseBody
	public Map<String,Object> index(
			JyTeaemployment stu, 
			@RequestParam(name = "page", defaultValue = "1") int pageNum,
			@RequestParam(name = "rows", defaultValue = "20") int pageSize){
		// 查询数据
		PageHelper.startPage(pageNum, pageSize);
		List<JyTeaemployment> pageData = stuinforMapper.list(stu);

		PageInfo<JyTeaemployment> info = new PageInfo<>(pageData);
		// 组装返回结果
		Map<String,Object> obj = new HashMap<String, Object>();
		obj.put("total", info.getTotal());
		obj.put("rows", info.getList());
		return obj;
	}

	/**
	 * 模糊查询学员
	 * 
	 * @return
	 */
	@RequestMapping("/getStudentList")
	@ResponseBody
	public List<Map<String, Object>> getStudentList() {
		List<Map<String, Object>> stu = stuinforMapper.selectStudent();
		return stu;
	}
	/**
	 * 删除归档学员记录
	 * @param id
	 * @return
	 */
	@GetMapping("delete/{id}")
	@ResponseBody
	@Transactional(rollbackFor = Exception.class)
	public Map<String, Object> delete(@PathVariable("id") Long id) {
		Map<String, Object> obj = new HashMap<String, Object>(16);
		try {
			obj.put("success", false);
			JyTeaemployment jy;
			if (id != null && (jy = teaDao.findOne(id)) != null) {
				if (jy.getOffer() != null) {
					File fileDelete = new File(uploadDir, jy.getOffer().getPath());
					fileDelete.delete();
					imgDao.delete(jy.getOffer().getId());
				}
				teaMapper.updateDelete(jy.getStudent().getId());
				obj.put("success", true);
			} else {
				obj.put("message", "此学员不存在！");
			}
		} catch (Exception e) {
			obj.put("message", "删除失败！");
		}
		return obj;
	}





}
